package com.ruoyi.web.controller.record;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.record.domain.WordGroupList;
import com.ruoyi.record.service.impl.WordRecordCommonService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.WordTestRecord;
import com.ruoyi.record.service.IWordTestRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单词测试记录Controller
 */
@RestController
@RequestMapping("/record/wordTest")
public class WordTestRecordController extends BaseController {
    @Autowired
    private IWordTestRecordService wordTestRecordService;

    @Autowired
    private WordRecordCommonService recordCommonService;

    /**
     * 查询单词测试记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WordTestRecord wordTestRecord) {
        WordTestRecord record = recordCommonService.selectList(wordTestRecord);
        startPage();
        List<WordTestRecord> list = wordTestRecordService.selectWordTestRecordList(record);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(WordTestRecord wordTestRecord) {
        List<WordTestRecord> list = wordTestRecordService.selectWordTestRecordList(wordTestRecord);
        return AjaxResult.success(list);
    }

    @GetMapping("/listSenior")
    public AjaxResult listSenior(WordTestRecord wordTestRecord) {
        List<WordTestRecord> list = wordTestRecordService.selectSeniorRecordList(wordTestRecord);
        return AjaxResult.success(list);
    }

    @GetMapping("/statistics/{date}/{userId}")
    public AjaxResult statisticsList(@PathVariable("date") String date, @PathVariable("userId") String userId) {
        AjaxResult ajaxResult = new AjaxResult();
        WordTestRecord record = new WordTestRecord();
        record.setTemp(date);
        logger.info("测试列表统计-请求参数:{}", record.getTemp());
        List<WordTestRecord> recordListAll = wordTestRecordService.selectWordTestRecordList(record);
        String deptName = recordCommonService.getDeptName(Long.valueOf(userId));
        record.setUserSchool(deptName);
        List<WordTestRecord> recordList = wordTestRecordService.selectWordTestRecordList(record);
        ajaxResult.put("todayNumAll", recordListAll.size());
        ajaxResult.put("todayNum", recordList.size());
        ajaxResult.put("todayRateAll", getRate(recordListAll) + "%");
        ajaxResult.put("todayRate", getRate(recordList) + "%");
        return AjaxResult.success(ajaxResult);
    }

    /**
     * 获得正确率
     */
    private BigDecimal getRate(List<WordTestRecord> recordList) {
        BigDecimal bigDecimal = new BigDecimal(0);
        if (recordList == null) {
            return new BigDecimal(0);
        }
        if (recordList.isEmpty()) {
            return new BigDecimal(0);
        }

        for (WordTestRecord record : recordList) {
            BigDecimal rate = record.getCorrectRate();
            bigDecimal = bigDecimal.add(rate);
        }
        BigDecimal decimal = bigDecimal.divide(new BigDecimal(recordList.size()), 2, RoundingMode.HALF_UP);
        logger.info("总正确率:{},最终结果:{}", bigDecimal, decimal);
        return decimal.multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP);
    }

    @GetMapping("/list_ByUser")
    public TableDataInfo listByUser(WordTestRecord wordTestRecord) {
        WordTestRecord record = recordCommonService.selectList(wordTestRecord);
        startPage();
        List<WordTestRecord> list = wordTestRecordService.selectWordTestRecordListByUser(record);
        return getDataTable(list);
    }

    /**
     * 导出单词测试记录列表
     */
//    @PreAuthorize("@ss.hasPermi('record:wordTest:export')")
    @Log(title = "单词测试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WordTestRecord wordTestRecord) {
        List<WordTestRecord> list = wordTestRecordService.selectWordTestRecordList(wordTestRecord);
        ExcelUtil<WordTestRecord> util = new ExcelUtil<WordTestRecord>(WordTestRecord.class);
        util.exportExcel(response, list, "单词测试记录数据");
    }

    /**
     * 获取单词测试记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('record:wordTest:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(wordTestRecordService.selectWordTestRecordById(id));
    }

    /**
     * 按日期分组
     */
    @GetMapping("/groupData")
    public AjaxResult getRecordGroupList() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("all", wordTestRecordService.selectRecordGroupList());
        ajaxResult.put("bySchool", wordTestRecordService.selectSchoolGroupList(null));
        ajaxResult.put("byStudent", wordTestRecordService.selectSchoolStudentGroupList(null));
        return AjaxResult.success(ajaxResult);
    }

    @GetMapping(value = {"/groupBySchool/{school:.*}","/groupBySchool"})
    public AjaxResult getRecordGroupList(@PathVariable(value = "school",required = false) String school) {
        AjaxResult ajaxResult = new AjaxResult();
        WordGroupList groupList = new WordGroupList();
        if (school != null && !school.isEmpty()) {
            groupList.setSchool(school);
        }
        ajaxResult.put("bySchool", wordTestRecordService.selectSchoolGroupList(groupList));
        ajaxResult.put("byStudent", wordTestRecordService.selectSchoolStudentGroupList(groupList));
        return AjaxResult.success(ajaxResult);
    }

    @GetMapping("/statisticsMore")
    public AjaxResult getRecordGroupMoreList() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("monthSchool", wordTestRecordService.selectSchoolMonthGroupList());
        ajaxResult.put("monthStudent", wordTestRecordService.selectSchoolMonthStudentGroupList());
        ajaxResult.put("weekSchool", wordTestRecordService.selectSchoolWeekGroupList());
        ajaxResult.put("weekStudent", wordTestRecordService.selectSchoolStudentWeekGroupList());
        return AjaxResult.success(ajaxResult);
    }

    /**
     * 新增单词测试记录
     */
//    @PreAuthorize("@ss.hasPermi('record:wordTest:add')")
    @Log(title = "单词测试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WordTestRecord wordTestRecord) {
        return toAjax(wordTestRecordService.insertWordTestRecord(wordTestRecord));
    }

    /**
     * 修改单词测试记录
     */
//    @PreAuthorize("@ss.hasPermi('record:wordTest:edit')")
    @Log(title = "单词测试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WordTestRecord wordTestRecord) {
        return toAjax(wordTestRecordService.updateWordTestRecord(wordTestRecord));
    }

    /**
     * 删除单词测试记录
     */
//    @PreAuthorize("@ss.hasPermi('record:wordTest:remove')")
    @Log(title = "单词测试记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(wordTestRecordService.deleteWordTestRecordByIds(ids));
    }
}
